#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
	if 0; #$running_under_some_shell

use File::Find ();
use File::Compare;
use File::Copy;
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

$skipdir='/org/lists.debian.org/archive-spam-removals/spam-removals';

umask 002;

# Traverse desired filesystems
File::Find::find({wanted => \&wanted},$skipdir);

die ("$0: needs to be run as user listweb\n") if ($ENV{'LOGNAME'} ne 'listweb');

foreach $spam_file_path (@files) {
  @spam_file = split('/', $spam_file_path);
  $file = $spam_file[$#spam_file];
  push (@{$spam_file{$file}}, $spam_file_path);
}

foreach $file (sort keys %spam_file) {
  if ($file =~ m#^([-\w]+)-(\d{4})(\d{2})?\.spam$#) {
    $list = $1;
    $year = $2;
    $month = $3 ? $3 : '';
    $dest_file = '/org/lists.debian.org/lists/' . $list . '/' . ($month ? $year : '') . '/' . $list . '-' . $year . $month;
    if (-e $dest_file) {
      open (DEST_FILE, "$dest_file") or die ("$0: can't open $dest_file for reading: $!\n");
      undef $multiline;
      while (<DEST_FILE>) {
	chomp;
	if (m#^Message-Id:\s*(.*)$#i) {
	  if ($1 ne '') {
	    push(@dest_msgid, $1);
	  } else {
	    $multiline=$_;
	  }
	} elsif ($multiline and m#^\s+(\S.*)$#i) {
	  push(@dest_msgid, $1);
	  undef $multiline;
	}
      }

      close (DEST_FILE);
      foreach $i (0..$#dest_msgid) {
	chomp($dest_msgid[$i]);
	$dest_msgid[$i] =~ s#^Message-ID:\s*##i;
	$dest_msgid[$i] =~ s#\s+.*$##;
	$dest_msgid[$i] =~ s#^<+##;
	$dest_msgid[$i] =~ s#>+\s*$##;
      }
      open (FILE_OUT, '>', "/tmp/$file") or die ("$0: can't open /tmp/$file for writing: $!\n");
      foreach $spam_file (sort @{$spam_file{$file}}) {
	open (FILE_IN, $spam_file) or die("$0: can't open $spam_file for reading: $!\n");
	foreach $line (<FILE_IN>) {
	  print FILE_OUT $line;
	  chomp $line;
	  $msgid = $line;
	  $msgid =~ m#^Skip-Spam-Message-Id:\s*<+([^<>]+)>#i;
	  $msgid = $1;
	  next unless $msgid;
	  $found = 0;
	  foreach $dest_msgid (@dest_msgid) {
	    if ($dest_msgid eq $msgid) {
	      $found = 1;
	      last;
	    }
	  }
	  unless ($found) {
	    print "$msgid from $spam_file NOT present in $dest_file\n";
	  }
	}
	close (FILE_IN);
      }
      close(FILE_OUT);
      if (-e $dest_file . '.spam') {
	if (compare("/tmp/$file", $dest_file . '.spam') == 0) {
	  unlink("/tmp/$file");
	} else {
	  system("/usr/bin/diff $dest_file.spam /tmp/$file");
	  move("/tmp/$file", $dest_file . '.spam') or die "$0: can't move /tmp/$file $dest_file.spam: $!\n";
	  system("/org/lists.debian.org/scripts/updatemail -a -f $dest_file");
	}
      } else {
	system("/usr/bin/diff /dev/null /tmp/$file");
	move("/tmp/$file", $dest_file . '.spam') or die "$0: can't move /tmp/$file $dest_file.spam: $!\n";
	system("/org/lists.debian.org/scripts/updatemail -a -f $dest_file");
      }
    } else {
      warn "$0: file does not exist: $dest_file from $file\n";
    }
  } else {
    warn "$0: wrong filename-format: $file\n";
  }
}

exit;

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    -f _ &&
    /^.*\.spam\z/s
    && push(@files, $name);
}

